package com.qf.controller;

import com.qf.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * 处理用户相关的接口
 */
@RestController
public class UserController {
    //固定写法：这里一定要写接口类型
    @Autowired//自动装配
    private UserSerivce userSerivce;//多态 用大的类型声明属性
    /**
     * 注册用户
     * @param username 用户名
     * @param pwd      密码
     * @return json
     */
    @RequestMapping("/register")
    public HashMap<String,Object> register(String username,String pwd){
        //把请求的参数打印到日志
        System.out.println("username");
        System.out.println("pwd");
        HashMap<String, Object> hashMap = new HashMap<>();
        if (username==null||"".equals(username)){
            hashMap.put("code","-1");
            hashMap.put("msg","用户名为空");
            return hashMap;
        }
        if (pwd==null||"".equals(pwd)){
            hashMap.put("code","-2");
            hashMap.put("msg","密码为空");
            return hashMap;
        }
        //用户注册具体逻辑 控制层把逻辑交给userserivce
        boolean result = userSerivce.register(username, pwd);
        if (result){
            //注册成功
            hashMap.put("code","1");
            hashMap.put("msg","注册成功");
        }else{
            hashMap.put("code","-1");
            hashMap.put("msg","注册失败");
        }
        return hashMap;
    }
    //登录
    @RequestMapping("/login")
    public HashMap<String,Object> login(String username,String pwd){
        System.out.println("username");
        System.out.println("pwd");
        HashMap<String, Object> hashMap = new HashMap<>();
        if (username==null||"".equals(username)){
            hashMap.put("code","-101");
            hashMap.put("msg","用户名不存在");
            return hashMap;
        }
        if (pwd==null||"".equals(pwd)){
            hashMap.put("code","-102");
            hashMap.put("msg","密码不存在");
            return hashMap;
        }
        boolean result = userSerivce.login(username,pwd);

        if(result){
            // 登录成功
            hashMap.put("code","2");
            hashMap.put("msg","登录成功");
        }else {
            // 注册失败
            hashMap.put("code","-103");
            hashMap.put("msg","登录失败");
        }

        return hashMap;
    }

}
